<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-11
 * Time: 14:54
 */
namespace app\common\model;

use think\Db;
use think\Model as ThinkModel;

/**
 *  技师点评  模型
**/
class UsersYelp extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'qt_users_yelp';
    protected $pk   = 'id';
    protected $autoWriteTimestamp = true;


    /**
     *  点评技师
     * @param int       $uv_id          用户ID
     * @param int       $engineer_id    技师ID
     * @param string    $content        内容
     * @param string    $tags           标签
     * @return boolean
    **/
    public function addYelp($uv_id,$engineer_id,$content = '',$tags  = '')
    {
        if(!$uv_id || !$engineer_id)
        {
            return false;
        }

        $add['uv_id']       =  $uv_id;
        $add['engineer_id'] =  $engineer_id;
        $add['content']     =  $content;
        $add['tags']        =  $tags;
        $add['create_time'] =  time();

        $res = $this->insert($add);

        if($res)
        {
            return true;
        }else{
            return false;
        }

    }

    /**
     *  单个技师点评列表
     * @param int $engineer_id  技师ID
     * @param int $limit        分页条数
     * @return array
    **/
    public function getYelpList($engineer_id,$limit = 10)
    {
        if(!$engineer_id)
        {
            return [];
        }

        $list = $this->where('engineer_id',$engineer_id)
            ->order('create_time desc')
            ->paginate($limit)
            ->each(function($item){
                $item['tags'] = strToArray($item['tags'],',');
                $user_viper = Db::name('qt_user_viper')->where('id',$item['uv_id'])->find();
                $item['user_name'] = !empty($user_viper['nickname']) ? $user_viper['nickname'] : '';
                $item['user_avatar'] = !empty($user_viper['avatar']) ? checkAvatar($user_viper['avatar']) : '';
                return $item;
            });

        return $list->items();
    }


}